const path = require('path');

function resolve (dir) {
  return path.join(__dirname, dir);
}
function getDateTime () {
    var d, s;
    d = new Date();
    s = d.getFullYear(); // 取年份
    s += '' + ('0' + (d.getMonth() + 1)).substr(-2);// 取月份
    s += '' + ('0' + d.getDate()).substr(-2); // 取日期
    s += '' + ('0' + d.getHours()).substr(-2); // 取小时
    s += '' + ('0' + d.getMinutes()).substr(-2); // 取分
    // s += ':' + ('0' + d.getSeconds()).substr(-2); // 取秒
    return s;
}

const proxyApi = {
  'dev': 'http://res.dev.linpeng99.com',
  'test': 'http://res.test.linpeng99.com',
}[process.env.VUE_APP_TITLE] || 'http://res.dev.linpeng99.com';

process.env.VUE_APP_BUILD_TIME = getDateTime();
process.env.VUE_APP_VERSION = require('./package.json').version + '_' + getDateTime();
process.env.VUE_APP_BASE_DOMAIN = process.env.NODE_ENV === 'production' ? '/' : proxyApi + '/';

module.exports = {
    css: {
        // extract: {
        //     // 为css添加版本号
        //     filename: `css/[name].${process.env.VUE_APP_VERSION}.css`,
        //     chunkFilename: `css/[name].${process.env.VUE_APP_VERSION}.css`,
        // },
        // sourceMap: process.env.NODE_ENV !== 'production',
        extract: false,
    },
  publicPath: process.env.NODE_ENV === 'production' ? './' : '/',
  pages: {
    index: {
      // entry for the page
      entry: 'beforeMain.js',
      // the source template
      template: 'public/index.html',
      // output as dist/index.html
      filename: 'index.html',
      // when using title option,
      // template title tag needs to be <title><%= htmlWebpackPlugin.options.title %></title>
      title: 'ocr',
      // chunks to include on this page, by default includes
      // extracted common chunks and vendor chunks.
      // chunks: ['chunk-vendors', 'chunk-common', 'index'],
    },
  },
  devServer: {
      proxy: {
          '/api': {
              target: proxyApi,
              changeOrigin: true,
              secure: false,
          },
      },
  },
  chainWebpack: (config) => {
    config.resolve.alias
      .set('$root', resolve(''))
      .set('$public', resolve('public'))
      .set('$dist', resolve('dist'))
      .set('@assets', resolve('src/assets'))
      .set('@components', resolve('photoQtiEditor/components'));
    // 这里只写了两个个，你可以自己再加，按这种格式.set('', resolve(''))
    config.module
        .rule('images')
        .use('url-loader')
        .tap(() => {
            return {
                limit: 4096,
                fallback: {
                    loader: 'file-loader',
                    options: {
                        name: `img/[name].${process.env.VUE_APP_VERSION}.[ext]`,
                    },
                },
            };
        });
    // 为js添加版本号
    config.output.chunkFilename(`js/[name].${process.env.VUE_APP_VERSION}.js`);
    config.output.filename(`js/[name].${process.env.VUE_APP_VERSION}.js`);
  },
};
